var adminInfo = JSON.parse(sessionStorage.getItem("adminInfo"));
var base64 = new Base64();
$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/synthesisFinancial/' + adminInfo.appId + '/list',
        datatype: "json",
        colModel: [
            {label: 'loanId', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '显示名称', name: 'showName', index: 'showName', width: 50},
            {
                label: 'icon',
                name: 'image',
                index: 'image',
                width: 50,
                formatter: function (value, options, rowObj) {
                    return '<img src=' + value + ' style="width:30px;height:30px;">';
                }
            },
            {
                label: '类型',
                name: 'type',
                index: 'type',
                width: 50,
                formatter: function (value, options, rowObj) {
                    if (value == 'recommend') {
                        return '<span class="label label-warning">推荐内容</span>';
                    } else if (value == 'category') {
                        return '<span class="label label-success">顶部类别</span>';
                    }
                }
            },
            {
                label: '组别',
                name: 'tag',
                index: 'tag',
                width: 50
            },
            {
                label: '状态',
                name: 'sfStatus',
                index: 'sfStatus',
                width: 50,
                formatter: function (value, options, rowObj) {
                    if (value == 'N') {
                        return '<span class="label label-warning">下架</span>';
                    } else if (value == 'Y') {
                        return '<span class="label label-success">正常</span>';
                    }
                }
            },
            {
                label: '排序', name: 'sortIndex', index: 'sortIndex', width: 50
            },
            {
                label: '链接地址', name: 'url', index: 'url', width: 80,
                formatter: function (value, options, rowObj) {
                    return '<a href="' + base64.decode(value) + '">链接地址</a>';
                }
            }
            // { label: '申请额度', name: 'loanMoney', index: 'loanMoney', width: 50 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "result.list",
            page: "result.currPage",
            total: "result.totalPage",
            records: "result.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条\
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    document.getElementById("iconImage").style.height = '70px';
    $("#iconImage").click(function () {
        $("#iconInput").click();
    });

    $("#iconInput").change(function () {
        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        if (objUrl != null) {
            $("#iconImage").attr("src", objUrl);
            vm.synthesisFinancial.image = objUrl;
        }
    });

});

function getObjectURL(file) {
    var url = null;
    // 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

var vm = new Vue({
    el: "#app",
    data: {
        showList: true,
        title: '',
        synthesisFinancial: {}
    },
    methods: {
        update: function () {
            var ids = getSelectedRows();

            if (ids.length > 1) {
                alert('不支持批量操作');
                return;
            } else if (ids.length == 0) {
                alert('请选择一条数据');
                return;
            }
            vm.showList = false;
            $.ajax({
                url: '/platform/manage/synthesisFinancial/info/' + ids[0],
                dataType: 'JSON',
                success: function (result) {
                    if (result.code == 200) {
                        vm.synthesisFinancial = result.result;
                        vm.synthesisFinancial.url = base64.decode(vm.synthesisFinancial.url);
                    } else {
                        alert(result.message, function () {
                            vm.reload();
                        });
                    }
                },
                error: function (e) {
                    console.error("error", e);
                    alert("请求失败", function () {
                        vm.reload();
                    });
                }
            });
        },
        add: function () {
            vm.synthesisFinancial = {};
            vm.showList = false;
        },
        saveOrUpdate: function () {
            Vue.delete(vm.synthesisFinancial, "image");
            if (vm.synthesisFinancial.tag === 'null' || vm.synthesisFinancial.tag === '') {
                vm.synthesisFinancial.tag = '';
            }
            vm.synthesisFinancial.url = base64.encode(vm.synthesisFinancial.url);
            var urlStr = vm.synthesisFinancial.id == null ? "/platform/manage/synthesisFinancial/" + adminInfo.appId + "/insert"
                : "/platform/manage/synthesisFinancial/" + adminInfo.appId + "/update";
            console.log("data=>"+JSON.stringify(vm.synthesisFinancial));
            $.ajaxFileUpload({
                url: urlStr,
                data: JSON.parse(JSON.stringify(vm.synthesisFinancial)),
                type: "POST",
                contentType: "multipart/form-data",
                secureuri: false,
                fileElementId: 'iconInput',
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        vm.reload();
                        alert("操作成功");
                    } else {
                        alert(result.message);
                        if (vm.synthesisFinancial.id == null) {
                            $("#iconImage").attr("src", "");
                        }
                    }
                },
                complete: function () {
                    $("#iconInput").replaceWith('<input type="file" style="display: none;" accept="image/*" name="file" id="iconInput" class="form-control" placeholder="file" />');
                    $("#iconInput").change(function () {
                        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
                        if (objUrl != null) {
                            $("#iconImage").attr("src", objUrl);
                            vm.synthesisFinancial.image = objUrl;
                        }
                    });
                },
                error: function (r, xml, states, e) {
                    console.log("error", e);
                    alert("请求失败，请重试");
                }
            });
        },
        reload: function () {
            vm.showList = true;
            vm.title = '';
            vm.synthesisFinancial = {};
            $("#jqGrid").jqGrid().trigger('reloadGrid');
        }
    }
});